package com.fyf.game.net.business.server;

import java.util.List;
import org.je.util.StringUtil;
import com.fyf.game.net.ReceiveHandler;
import com.fyf.game.net.Sender;
import com.fyf.game.net.business.Cmd;

/**
 * @author fanyifeng
 */
public class ServerReceiveHandler implements ReceiveHandler
{
	private final LoginLogoutHandler	loginLogout;
	private final ChartHandler			chart;

	public ServerReceiveHandler() {
		loginLogout = new LoginLogoutHandler();
		chart = new ChartHandler();
	}

	public void onReceive(String msg, Sender sender) {
		List<String> list = StringUtil.split(msg ,Cmd.spr);
		String cmd = list.get(0);
		if (cmd.equals(Cmd.chart)) {
			chart.doChart(list.get(1) ,list.get(2) ,sender);
		} else if (cmd.equals(Cmd.login)) {
			loginLogout.doLogin(list.get(1) ,list.get(2) ,list.get(3) ,sender);
		} else if (cmd.equals(Cmd.logout)) {
			loginLogout.doLogout(sender);
		} else if (cmd.equals(Cmd.scan_user)) {
			doScanUser(sender);
		}
	}

	private void doScanUser(Sender sender) {
		StringBuilder bf = new StringBuilder();
		for (Sender sd : sender.getBox().getAllSenders()) {
			bf.append(sd.getID() + "@" + sd.getName() + "#");
		}
		sender.send(Cmd.scan_user + Cmd.spr + bf.toString().trim());
	}
}
